import { Coord } from "./coord";

export class AteRecord {
  static readonly key = 'ateRecord'

  static readonly coordList = AteRecord.load()

  static load() {
    const strings: string[] = JSON.parse(localStorage.getItem(AteRecord.key) ?? '[]')
    const list = strings.map(s => {
      const [row, col] = s.split(',', 2).map(x => parseInt(x))
      return new Coord(row, col)
    })
    return new Set(list)
  }

  static save() {
    const strings = [...AteRecord.coordList].map(({ row, col }) => `${row},${col}`);
    localStorage.setItem(AteRecord.key, JSON.stringify(strings))
  }

  static append(...coord: Coord[]) {
    for (const x of coord) {
      AteRecord.coordList.add(x)
    }
    AteRecord.save()
  }

  static clear() {
    AteRecord.coordList.clear()
    AteRecord.save()
  }
}
